package com.example.mybatisplus.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mybatisplus.mapper.ResidentMapper;
import com.example.mybatisplus.model.domain.Bed;
import com.example.mybatisplus.mapper.BedMapper;
import com.example.mybatisplus.model.dto.BedInfoDTO;
import com.example.mybatisplus.model.dto.PageDTO;
import com.example.mybatisplus.model.domain.Building;
import com.example.mybatisplus.model.dto.UnderDTO;
import com.example.mybatisplus.service.BedService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lxp
 * @since 2024-07-17
 */
@Service
public class BedServiceImpl extends ServiceImpl<BedMapper, Bed> implements BedService {
    @Autowired
    private BedMapper BedMapper;

    @Override
    public Page<BedInfoDTO> getDisabledBed(BedInfoDTO bedInfoDTO, PageDTO dto) {
        Page<BedInfoDTO> page = new Page<>();
        page.setCurrent(dto.getPageNo()).setSize(dto.getPageSize());
        baseMapper.findDisabledBed(page, bedInfoDTO.getAreaName(), bedInfoDTO.getBuildingName(), bedInfoDTO.getFloor());
        return page;
    }

    public int getRestBed(String areaname, String campusname) {
        return BedMapper.getRestBed(areaname, campusname);
    }

    public int getMasterBed(String areaname, String campusname) {
        return BedMapper.getMasterBed(areaname, campusname);
    }

    public int getDoctorBed(String areaname, String campusname) {
        return BedMapper.getDoctorBed(areaname, campusname);
    }

    public int getUndergBed(String areaname, String campusname) {
        return BedMapper.getUndergBed(areaname, campusname);
    }

    public int getTeacherBed(String areaname, String campusname) {
        return BedMapper.getTeacherBed(areaname, campusname);
    }

    //房源分配
    public int getMasterCount() {
        return BedMapper.getMasterCount();
    }

    public int getDoctorCount() {
        return BedMapper.getDoctorCount();
    }

    public int getUndergCount() {
        return BedMapper.getUndergCount();
    }

    public int getTeacherCount() {
        return BedMapper.getTeacherCount();
    }

    public int getFreeMasterCount() {
        return BedMapper.getMasterCount();
    }

    public int getFreeDoctorCount() {
        return BedMapper.getDoctorCount();
    }

    public int getFreeUndergCount() {
        return BedMapper.getUndergCount();
    }

    public int getFreeTeacherCount() {
        return BedMapper.getTeacherCount();
    }

    @Override
    public boolean releaseBed(Bed bed) {
        QueryWrapper<Bed> wrapper = new QueryWrapper<>();
        wrapper.eq("bedId", bed.getBedId());
        Bed one = baseMapper.selectOne(wrapper);
        one.setIsEnabled(0);
        baseMapper.updateById(one);
        return true;
    }
    //

    @Override
    public Page<UnderDTO> getUnderBed(UnderDTO underDTO, PageDTO dto) {
        Page<UnderDTO> page = new Page<>();
        page.setCurrent(dto.getPageNo()).setSize(dto.getPageSize());
        baseMapper.findUnderBed(page, underDTO.getAreaName(), underDTO.getBuildingName(), underDTO.getFloor());
        return page;
    }

    @Override
    public Page<UnderDTO> getPostBed(UnderDTO underDTO, PageDTO dto) {
        Page<UnderDTO> page = new Page<>();
        page.setCurrent(dto.getPageNo()).setSize(dto.getPageSize());
        baseMapper.findPostBed(page, underDTO.getAreaName(), underDTO.getBuildingName(), underDTO.getFloor());
        return page;
    }

    @Override
    public Page<UnderDTO> getStaffBed(UnderDTO underDTO, PageDTO dto) {
        Page<UnderDTO> page = new Page<>();
        page.setCurrent(dto.getPageNo()).setSize(dto.getPageSize());
        baseMapper.findStaffBed(page, underDTO.getAreaName(), underDTO.getBuildingName(), underDTO.getFloor());
        return page;

    }
    @Override
    public void moveBeds(List<Integer> bedIds, String target) {
        String bedtype;
        switch (target) {
            case "underg":
                bedtype = "underg";
                break;
            case "master":
                bedtype = "master";
                break;
            case "teacher":
                bedtype = "teacher";
                break;
            default:
                throw new IllegalArgumentException("无效的目标类型");
        }
        BedMapper.updateBedTypeAndStatus(bedIds, bedtype, 1);
    }
}
